package ian.android.lovewords.words;

import java.util.ArrayList;

public class WordSchedule {
	private static int AllListNumber = 50;
	private static int ListsEveryday = 2;
	
	private int NewDay;
	private int AllDay;
	
	private int[][] NewWordSchedule;
	private int[][] ReviewSchedule;
	
	public WordSchedule (int AllList, int Everyday)
	{
		AllListNumber = AllList;
		ListsEveryday = Everyday;
		ComputeNewWords();
		ComputeReviewWords();
	}
	
	public WordSchedule ()
	{
		ComputeNewWords();
		ComputeReviewWords();
	}
	
	public int getNewDay()
	{
		return NewDay;
	}
	
	public static void setAllListNum(int AllNum)
	{
		AllListNumber = AllNum;
	}
	
	public int getAllListNum()
	{
		return AllListNumber;
	}
	
	public static void setListEveryday(int listEveryday)
	{
		ListsEveryday = listEveryday;
	}
	
	public int getListEveryday()
	{
		return ListsEveryday;
	}
	
	public int getAllDay()
	{
		return AllDay;
	}
	
	private void ComputeNewWords()
	{
		NewDay =  AllListNumber/ListsEveryday +1;
		NewWordSchedule = new int[ NewDay ][ListsEveryday];
		int i,j;
		for( i = 0; i < NewDay ; i++ )
		{
			for( j = 0; j < ListsEveryday; j++ )
			{
				NewWordSchedule[i][j] = i * ListsEveryday + j + 1; 
			}
		}
	}
	
	private void ComputeReviewWords()
	{
		AllDay = NewDay + 29;
		ReviewSchedule = new int[AllDay][5 * ListsEveryday];
		int i,j;
		for( i = 0; i < AllDay ; i++)
		{
			for( j = 0; j < 5 * ListsEveryday; j++ )
			{
				ReviewSchedule[i][j] = 0;
			}
		}
		for( i = 0; i < NewDay ; i++)
		{
			for( j = 0; j < ListsEveryday; j++ )
			{
				int k;
				for( k = 0; k < 5*ListsEveryday; k++ )
					if( ReviewSchedule[i+1][k] == 0 )
						break;
				ReviewSchedule[i+1][k] = NewWordSchedule[i][j];
				for( k = 0; k < 5*ListsEveryday; k++ )
					if( ReviewSchedule[i+3][k] == 0 )
						break;
				ReviewSchedule[i+3][k] = NewWordSchedule[i][j];
				for( k = 0; k < 5*ListsEveryday; k++ )
					if( ReviewSchedule[i+7][k] == 0 )
						break;
				ReviewSchedule[i+7][k] = NewWordSchedule[i][j];
				for( k = 0; k < 5*ListsEveryday; k++ )
					if( ReviewSchedule[i+14][k] == 0 )
						break;
				ReviewSchedule[i+14][k] = NewWordSchedule[i][j];
				for( k = 0; k < 5*ListsEveryday; k++ )
					if( ReviewSchedule[i+29][k] == 0 )
						break;
				ReviewSchedule[i+29][k] = NewWordSchedule[i][j];
			}
		}
	}
	
	public int[] getNewWord( int day )
	{
		if(day > NewDay)
			return new int[0];
		return NewWordSchedule[day - 1];
	}
	
	public ArrayList<Integer> getReviewWord( int day )
	{
		ArrayList<Integer> mArray = new ArrayList<Integer>(); 
		if(day > AllDay)
			return mArray;
		int i;
		for( i=0; i< 5 * ListsEveryday; i++ )
		{
			if( ReviewSchedule[day - 1][i] != 0 )
				mArray.add( ReviewSchedule[day - 1][i]);
		}
		return mArray;
	}
}
